package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.TResult;
import com.sky.happygos.R;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.CreateShopE;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateShopE;
import com.sky.happygos.model.Shop;
import com.sky.happygos.present.ShopP;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/22.
 */
@EActivity(R.layout.activity_shop_detail)
public class ShopCreateA extends TakePhotoActivity {

    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.sdv_shop_edit_icon)
    SimpleDraweeView icon;

    @ViewById(R.id.et_shop_name)
    EditText name;
    @ViewById(R.id.et_shop_info)
    EditText info;
    @ViewById(R.id.et_shop_school)
    EditText school;
    @ViewById(R.id.et_shop_addr)
    EditText addr;
    @ViewById(R.id.et_shop_notice)
    EditText notice;
    @ViewById(R.id.header_right)
    ImageView commit;
    @ViewById
    TextView title;


    @Bean
    ShopP shopP;


    String path = null;
    boolean isChange = false;

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            isChange = true;
            commit.setVisibility(View.VISIBLE);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


    @AfterViews
    public void init(){
        if(title != null) {
            title.setText("创建商店");
        }
        loadData();
    }

    @Click(R.id.header_right)
    public void commit(){
        Shop s = new Shop();
        s.name = name.getText().toString();
        s.info = info.getText().toString();
        s.school = school.getText().toString();
        s.addr = addr.getText().toString();
        loadView.start();
        shopP.createShop(path,s);
    }

    @Click(R.id.sdv_shop_edit_icon)
    public void clickPhoto(){
        TakePhoto takePhoto = getTakePhoto();
        CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(600*800).setMaxPixel(800).create();
        takePhoto.onEnableCompress(compressConfig,true);
        takePhoto.onPickFromGallery();
    }

    public void loadData(){
//        icon.setImageURI(ImagePath.to(shop.icon));
//        name.setText(shop.name);
//        info.setText(shop.info);
//        school.setText(shop.school);
//        addr.setText(shop.addr);
//        if(shop.notice != null && shop.notice.size() > 0) {
//            notice.setText(shop.notice.get(shop.notice.size()-1));
//        }else{
//            notice.setHint("未发布公告");
//        }

        name.addTextChangedListener(watcher);
        info.addTextChangedListener(watcher);
        school.addTextChangedListener(watcher);
        addr.addTextChangedListener(watcher);
//        notice.addTextChangedListener(watcher);
    }


    @Subscribe
    public void createShopResult(CreateShopE e){
        loadView.dismiss();
        if(e.isSuccess){
            Toast.makeText(this,"创建成功",Toast.LENGTH_SHORT).show();
            finish();
        }else{
            Toast.makeText(this,"创建失败",Toast.LENGTH_SHORT).show();
        }
    }


    @Click(R.id.header_left)
    public void back(){
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        path = result.getImage().getCompressPath();
        icon.setImageURI(String.format("file://%s",result.getImage().getCompressPath()));
        isChange = true;
        commit.setVisibility(View.VISIBLE);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(this,"获取失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        Toast.makeText(this,"获取取消",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
